<?php
$error_msg['RC002'] = 'Indique um nome de beneficiario';
$error_msg['RC004'] = 'Indique um numero de documento.';
$error_msg['RC007'] = 'Indique uma cidade';
$error_msg['RC008'] = 'Indique um estado.';
$error_msg['RC009'] = 'Benficiario parceiro.';

function opv_docnumberformat($doctype, $docnumber) {
    $formated_docnumber = preg_replace("#[\-\/\.\ \,]#", "", $docnumber);
    if (in_array($doctype, array(0, 7)) && strlen($formated_docnumber) == 11) {
        $formated_docnumber = substr($formated_docnumber, 0, 3) . '.' . substr($formated_docnumber, 3, 3) . '.' . substr($formated_docnumber, 6, 3) . '-' . substr($formated_docnumber, 9, 2);
    } elseif ($doctype == 8 && strlen($formated_docnumber) == 14) {
        $formated_docnumber = substr($formated_docnumber, 0, 2) . '.' . substr($formated_docnumber, 2, 3) . '.' . substr($formated_docnumber, 5, 3) . '/' . substr($formated_docnumber, 8, 4) . '-' . substr($formated_docnumber, 12, 2);
    }
    return $formated_docnumber;
}

function GetReceiver($ReceiverID) {

}

function ListReceivers($ViewMode, $ReceiverService, $SenderID, $ReceiverID) {
    $FloatQur = ' AND BnfBloqueado = 0';
    if ($ViewMode == 1) {
        $FloatQur = '';
    }
    if ($ReceiverID > 0) {
        $FloatQur .= ' AND BnfNumero = ' . $ReceiverID;
    }
    if ($ReceiverService == 0) {
        $qrCondition = "(UniCliente = $SenderID $FloatQur AND UniCliente > 0)";
    } else {
        $qrCondition = "(BnfNumExt > 0 AND BnfExtTransactionType = $ReceiverService)";
    }
    $GET = $GLOBALS['MYT']->db_array("SELECT BnfNumero,
	 					 BnfNombre AS ReceiverName,
						 BnfCedula,
						 BnfDireccion,
						 BnfBarrio,
						 BnfCiudad,
						 BnfProvincia,
						 hd_paises.pai_NombreESP AS BnfPais,
						 BnfTelefono,
						 BnfTelefono2,
						 BnfBloqueado,
						 BnfPais AS BnfPaisCode,
						 BnfNotes,
						 BnfDocType,
						 uniRelation
				FROM beneficiarios
	 				INNER JOIN hd_paises ON pai_CodElite = BnfPais
					LEFT JOIN beneficiariosinstitucionais ON BnfExtNumero = BnfNumero
					LEFT JOIN unionclientesbeneficiarios ON BnfNumero = UniBeneficiario
					WHERE
					$qrCondition
					GROUP BY BnfNumero
					ORDER BY BnfNombre ");
    return $GET;
}

function GridReceivers($GET, $ViewMode, $ReceiverID) {
    $MDRC = 'RD';
    $CGET = $GET[0];
    for ($r = 1; $r <= $CGET; $r++) {
        $nvstr = '';
        $bnfprt = '';
        if ($GET[$r]['uniRelation'] != '' || $ViewMode > 0) {
            $bnfprt = '" disabled readonly="';
        }
        if ($GET[$r]['BnfBloqueado'] == 0 && ($GET[$r]['BnfCedula'] != '' || $GET[$r]['BnfPaisCode'] != 76)) {
            $CheckRows = 'bgcolor="#DADADA" class="Selector"
		onMouseOver="this.className=\'Selector_Over\'"
		onMouseOut="this.className=\'Selector\'"
		onClick="this.className=\'Selector_Click\';';
            $nvstr = 'onClick="document.ReceiverProperties.RID.value=\'' . $GET[$r]['BnfNumero'] . '\';
		document.ReceiverProperties.action=\'_response/call.php?get=Operative/receiver\';
		subform(\'ReceiverProperties\',\'ClientProp\');"';
        } else {
            $CheckRows = 'bgcolor="#CCCCCC';
        }
        echo '<tr ' . $CheckRows . '">';
        if ($ViewMode == 0) {
            echo '<td onClick="document.ReceiverProperties.RID.value=' . $GET[$r]['BnfNumero'] . ';
		document.ReceiverProperties.action=\'_response/call.php?get=Operative/view/viewreceiver\';
		subform(\'ReceiverProperties\',\'NWRC\');">' . rt_field('hidden', 'ED', 'OBJCTRC', $GET[$r]['BnfNumero'], '', '', '', '') . '<img src="_response/bin.php?get=edit.gif"></td>';
        } else {
            echo '<td></td>';
        }
        echo '<td ' . $nvstr . '>' . rt_field('text', $MDRC, 'BnfNumero', $GET[$r]['BnfNumero'], '10', '8', '', '') . '</td>
    <td ' . $nvstr . ' NOWRAP>' . rt_field('text', $MDRC, 'Name', $GET[$r]['ReceiverName'], '40', '35', 'True', '') . '</td>
    <td ' . $nvstr . ' NOWRAP>' . rt_field('text', $MDRC, 'Name', opv_docnumberformat($GET[$r]['BnfDocType'], $GET[$r]['BnfCedula']), '40', '35', 'True', '') . '</td>
    <td ' . $nvstr . ' NOWRAP>' . rt_field('text', $MDRC, 'Name', $GET[$r]['BnfPais'], '40', '35', 'True', '') . '</td>
    <td ' . $nvstr . ' NOWRAP>' . rt_field('text', $MDRC, 'Name', $GET[$r]['BnfTelefono'], '40', '35', 'True', '') . '</td>
    <td ' . $nvstr . ' NOWRAP>' . rt_field('text', $MDRC, 'Name', $GET[$r]['BnfNotes'], '40', '35', 'True', '') . '</td>
    <td NOWRAP>' . rt_field('select', 'ED', 'uniRelation', $GET[$r]['uniRelation'], "SELECT stpAreaID AS ID, stpName AS NAME FROM hf_systypes WHERE stpGroup = 3 AND stpFlag = 2 ORDER BY NAME", ' ', '', '" onchange="
    	if(this.options[this.selectedIndex].value==10){
	idwindow(\'Indique qual a relacao\',\'rcv_prt\',\'rcv_prt\',\'_response/call.php?get=Operative/view/receiverrelatives&r=' . $r . '&rel1=' . $ReceiverID . '&rel2=' . $GET[$r]['BnfNumero'] . '&rel=\'+this.options[this.selectedIndex].value,500,60);
	} else {
	this.disabled=true;
    	replaceUrl(\'rsBnf' . $r . '\',\'_response/call.php?get=Operative/opv_receiver&rel1=' . $ReceiverID . '&rel2=' . $GET[$r]['BnfNumero'] . '&rel=\'+this.options[this.selectedIndex].value);}" style="width:80px"' . $bnfprt) . '</td>
    <td><div id="rsBnf' . $r . '"></div></td>
  </tr>';
    }
}

function ListReceiverDetails($ViewMode, $ReceiverID) {
    $MDRC = 'RD';



    if ($ReceiverID > 0) {
        $Receiver = $GLOBALS['MYT']->db_array("SELECT BnfNumero,
         CONCAT(BnfNombre,' ',BnfAPellidos) AS ReceiverName,
         BnfCedula,
         BnfDireccion,
         BnfBarrio,
         BnfCiudad,
         Prov_Provincia AS BnfProvincia,
         BnfPais,
         BnfTelefono,
         BnfTelefono2
    FROM beneficiarios
            LEFT OUTER JOIN cp_provincias ON Prov_ID = BnfProvincia
            WHERE BnfNumero = $ReceiverID");
        for ($rc = 1; $rc <= $Receiver[0]; $rc++) {
?>
            <div id="RCDT">
                <table width="100%" border="0" cellpadding="1" cellspacing="1" bgcolor="#BACCD1">
                    <tr>
                        <td colspan="3">Morada</td>
                        <td width="120">Bairro</td>
                        <td width="200">Localidade</td>
                        <td width="200">Estado</td>
                    </tr>
                    <tr>
                        <td colspan="4"><?php mn_field('text', $MDRC, 'BnfDireccion', $Receiver[$rc]['BnfDireccion'], '10', '8', 'True', ''); ?></td>
                        <td><?php mn_field('text', $MDRC, 'BnfBarrio', $Receiver[$rc]['BnfBarrio'], '40', '35', 'True', ''); ?></td>
                        <td><?php mn_field('text', $MDRC, 'BnfCiudad', $Receiver[$rc]['BnfCiudad'], '40', '35', 'True', ''); ?></td>
                        <td><?php mn_field('text', $MDRC, 'BnfProvincia', $Receiver[$rc]['BnfProvincia'], '10', '8', 'True', ''); ?></td>
                    </tr>
                </table>
            </div>
            <div id="AC" class="Line">
                <div id="NWAC"></div>
                <div>Modo de Pagamento <div id="inv_MP" style="float:right">
                        <?php include('conta.php'); ?></div>
                </div>
    <div id="AC_List"></div>
</div>
<?php
        }
    }
}
?>
